package com.jf.cloud.product.controller.app;

import com.jf.cloud.common.product.dto.RankDTO;
import com.jf.cloud.product.model.SpuRank;
import com.jf.cloud.product.service.SpuRankService;
import com.jf.cloud.product.vo.SpuRankVO;
import com.jf.cloud.product.dto.SpuRankDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 *
 * @author zz
 * @date 2023-08-14 10:34:19
 */
@RestController("appSpuRankController")
@RequestMapping("/spu_rank")
@Tag(name = "")
public class SpuRankController {

    @Autowired
    private SpuRankService spuRankService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<List<SpuRank>> page(@ParameterObject SpuRankDTO spuRankDTO) {
        List<SpuRank> spuRankPage = spuRankService.listAll(spuRankDTO.getSpuId(),spuRankDTO.getSkuId());
        return ServerResponseEntity.success(spuRankPage);
    }

    @GetMapping("/checkStatus")
    @Operation(summary = "校验是否存在已经购买的", description = "校验是否存在已经购买的")
    public ServerResponseEntity<Boolean> checkStatus(@RequestParam String ids) {

        return ServerResponseEntity.success(spuRankService.checkStatus(ids));
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<SpuRank> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(spuRankService.getById(id));
    }

    @GetMapping("by_order_id")
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<List<SpuRank>> getByOrderId(@RequestParam Long orderId) {
        return ServerResponseEntity.success(spuRankService.getByOrderId(orderId));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SpuRankDTO spuRankDTO) {
        SpuRank spuRank = mapperFacade.map(spuRankDTO, SpuRank.class);
        spuRankService.save(spuRank);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SpuRankDTO spuRankDTO) {
        SpuRank spuRank = mapperFacade.map(spuRankDTO, SpuRank.class);
        spuRankService.update(spuRank);
        return ServerResponseEntity.success();
    }

    @GetMapping("batchUpdate")
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> batchUpdate(@RequestParam String ids,@RequestParam Long orderId,@RequestParam Integer status) {
        for (int i = 0; i < ids.split(",").length; i++) {
            SpuRank spuRank=spuRankService.getById(Long.valueOf(ids.split(",")[i]));
            spuRank.setStatus(status);
            spuRank.setOrderId(orderId);
            spuRankService.update(spuRank);
        }
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        spuRankService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
